package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/17.
 */

public class CommentItem extends BindingListItem {
    private int commentid;
    private int uid;
    private String photo;
    private String name;
    private String commentcontent;
    private String job;
    private String industry;
    private String avatar;
    //    private List<String>
    private String commentType;
    private Date commenttime;
    private int reply;
    private int commentlike;
    private ListBindingAdapter replyList;

    public int getCommentid() {
        return commentid;
    }

    public void setCommentid(int commentId) {
        this.commentid = commentId;
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Bindable
    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    public String getCommentType() {
        return commentType;
    }

    public void setCommentType(String commentType) {
        this.commentType = commentType;
    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getCommentcontent() {
        return commentcontent;
    }

    public void setCommentcontent(String commentcontent) {
        this.commentcontent = commentcontent;
        notifyPropertyChanged(BR.commentcontent);
    }

    @Bindable
    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
        notifyPropertyChanged(BR.photo);
    }

    @Bindable
    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @Bindable
    public int getReply() {
        return reply;
    }

    public void setReply(int reply) {
        this.reply = reply;
        notifyPropertyChanged(BR.reply);
    }

    @Bindable
    public Date getCommenttime() {
        return commenttime;
    }

    public void setCommenttime(Date commenttime) {
        this.commenttime = commenttime;
        notifyPropertyChanged(BR.commenttime);
    }

    @Bindable
    public int getCommentlike() {
        return commentlike;
    }

    public void setCommentlike(int like) {
        this.commentlike = like;
        notifyPropertyChanged(BR.commentlike);
    }

    public CommentItem(Context context, int layoutId) {
        super(context, layoutId);
    }

//    public void loadReplyList(){
//        if(replyList == null){
//            replyList = new ListBindingAdapter<ReplyItem>(getContext(), BR.comment);
//        }
//        final Activity activity = (Activity)getContext();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                replyList.setNotifyOnChange(false);
//                ArrayList result = WebApi.getReplyCommentList(getContext(), getCommentid());
//                try {
//                    for(Object item : result){
//                        LinkedTreeMap map = (LinkedTreeMap)item;
//                        ReplyItem replyItem = new ReplyItem(activity, R.layout.i_comment);
//                        //        "id":"2","uid":"10","commentid":"1","replycontent":"000000","replytime":"2016-11-25 14:46:20"
//                        replyItem.setId(Utils.getIntValue(map.get("id")));
//                        replyItem.setUid(Utils.getIntValue(map.get("uid")));
//                        replyItem.setCommentid(Utils.getIntValue(map.get("commentid")));
//                        replyItem.setCommentcontent((String)map.get("replycontent"));
//                        replyItem.setCommenttime(Utils.getDateFromFormat(map.get("replytime"), "yyyy-MM-dd HH:mm:ss"));
//                        replyList.add(replyItem);
//                    }
//                }catch (Exception ex){
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(activity, "解析数据时发生错误", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                }
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        replyList.notifyDataSetChanged();
//                    }
//                });
//            }
//        }).start();
//    }

    /**
     * 点击评论内容，显示本条评论所有回复
     *
     * @param view
     */
    public void onClickItem(View view) {
        CommonFragment fragment = new CommonFragment();
        ReplyListModel model = new ReplyListModel(getContext(), this);
        fragment.setDataModel(model);
        fragment.setVariableId(BR.reply);
        fragment.setLayoutId(R.layout.f_reply_list);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    public void onClickAvatar(View view) {
        HomeActivity.showPersonalCentral(getContext(), uid);
    }

    /**
     * 评论点赞
     *
     * @param view
     */
    public void onClickLike(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = WebApi.addLikeComment(getContext(), getCommentid());
                if (count < 0)
                    return;
                setCommentlike(count);
            }
        }).start();
    }
}
